﻿
namespace OP10.ViewModels
{
    public class ST04ElectrodeNeedleOperateViewModel : BindableBase
    {

        private readonly IRegionManager _regionManager;

        public ST04ElectrodeNeedleOperateViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;
            InitParam();
            //开启定时器任务
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(200);
            dispatcherTimer.Tick += OnTick;
            dispatcherTimer.Start();
        }

        private void OnTick(object? sender, EventArgs e)
        {
            WireCurrentLen = WirePLCToPC_Global.WireCurrentLen;
            LeftPos = LeftMotorPLCToPC_Global.ActutalPositon;
            RightPos = RightMotorPLCToPC_Global.ActutalPositon;

            XCurrentPos = XAxisServo.ServoPLCToPC_Global.nActualPosition;
            YLCurrentPos = YAxisLeftServo.ServoPLCToPC_Global.nActualPosition;
            YRCurrentPos = YAxisRightServo.ServoPLCToPC_Global.nActualPosition;
        }

        private void InitParam()
        {

            WireLenChangSpeed = WireParam_Global.WireLenChangSpeed;
            LeftCheckPos = LeftMotorParam_Global.CheckPosition;
            RightCheckPos = RightMotorParam_Global.CheckPosition;
            LeftWaitPos = LeftMotorParam_Global.WaitPostion;
            RightWaitPos = RightMotorParam_Global.WaitPostion;
            DefineSpacing = WireParam_Global.WireDerictLen;
            LeftWorkPos = (float)LeftMotorParam_Global.WorkPosition / 10000;
            RightWorkPos = (float)RightMotorParam_Global.WorkPosition / 10000;
            CheckPinGap = WireParam_Global.CheckPinGap;
        }



        #region 事件
        private DelegateCommand _GoBackCommand;

        public DelegateCommand GoBackCommand =>
            _GoBackCommand ?? (_GoBackCommand = new DelegateCommand(ExecuteGoBackCommand));

        void ExecuteGoBackCommand()
        {
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST04DeviceManagerView);
        }




        private DelegateCommand _WireLenMinDownCmd;
        public DelegateCommand WireLenMinDownCmd =>
            _WireLenMinDownCmd ?? (_WireLenMinDownCmd = new DelegateCommand(ExecuteWireLenMinDownCmd));

        void ExecuteWireLenMinDownCmd()
        {
            WirePCToPLC_Global.WireLenMinus = true;
        }

        private DelegateCommand _WireLenMinUpCmd;
        public DelegateCommand WireLenMinUpCmd =>
            _WireLenMinUpCmd ?? (_WireLenMinUpCmd = new DelegateCommand(ExecuteWireLenMinUpCmd));

        void ExecuteWireLenMinUpCmd()
        {
            WirePCToPLC_Global.WireLenMinus = false;

        }


        private DelegateCommand _WireLenAddDownCmd;
        public DelegateCommand WireLenAddDownCmd =>
            _WireLenAddDownCmd ?? (_WireLenAddDownCmd = new DelegateCommand(ExecuteWireLenAddDownCmd));

        void ExecuteWireLenAddDownCmd()
        {
            WirePCToPLC_Global.WireLenPlus = true;

        }

        private DelegateCommand _WireLenAddUpCmd;
        public DelegateCommand WireLenAddUpCmd =>
            _WireLenAddUpCmd ?? (_WireLenAddUpCmd = new DelegateCommand(ExecuteWireLenAddUpCmd));

        void ExecuteWireLenAddUpCmd()
        {
            WirePCToPLC_Global.WireLenPlus = false;

        }
        private DelegateCommand _WireDerictLenGODownCmd;
        public DelegateCommand WireDerictLenGODownCmd =>
            _WireDerictLenGODownCmd ?? (_WireDerictLenGODownCmd = new DelegateCommand(ExecuteWireDerictLenGODownCmd));

        void ExecuteWireDerictLenGODownCmd()
        {
            WirePCToPLC_Global.WireDerictLenGO = true;
        }


        private DelegateCommand _WireDerictLenGOUpCmd;
        public DelegateCommand WireDerictLenGOUpCmd =>
            _WireDerictLenGOUpCmd ?? (_WireDerictLenGOUpCmd = new DelegateCommand(ExecuteWireDerictLenGOUpCmd));

        void ExecuteWireDerictLenGOUpCmd()
        {
            WirePCToPLC_Global.WireDerictLenGO = false;

        }
        private DelegateCommand _WireJodUpDownCmd;
        public DelegateCommand WireJodUpDownCmd =>
            _WireJodUpDownCmd ?? (_WireJodUpDownCmd = new DelegateCommand(ExecuteWireJodUpDownCmd));

        void ExecuteWireJodUpDownCmd()
        {
            WirePCToPLC_Global.WireJodPlus = true;
        }

        private DelegateCommand _WireJodUpUpCmd;
        public DelegateCommand WireJodUpUpCmd =>
            _WireJodUpUpCmd ?? (_WireJodUpUpCmd = new DelegateCommand(ExecuteWireJodUpUpCmd));

        void ExecuteWireJodUpUpCmd()
        {
            WirePCToPLC_Global.WireJodPlus = false;

        }


        private DelegateCommand _WireJodDownDownCmd;
        public DelegateCommand WireJodDownDownCmd =>
            _WireJodDownDownCmd ?? (_WireJodDownDownCmd = new DelegateCommand(ExecuteWireJodDownDownCmd));

        void ExecuteWireJodDownDownCmd()
        {
            WirePCToPLC_Global.WireJodMinus = true;
        }


        private DelegateCommand _WireJodDownUpCmd;
        public DelegateCommand WireJodDownUpCmd =>
            _WireJodDownUpCmd ?? (_WireJodDownUpCmd = new DelegateCommand(ExecuteWireJodDownUpCmd));

        void ExecuteWireJodDownUpCmd()
        {
            WirePCToPLC_Global.WireJodMinus = false;

        }

        private DelegateCommand _BackCheckPosDownCmd;
        public DelegateCommand BackCheckPosDownCmd =>
            _BackCheckPosDownCmd ?? (_BackCheckPosDownCmd = new DelegateCommand(ExecuteBackCheckPosDownCmd));

        void ExecuteBackCheckPosDownCmd()
        {
            WirePCToPLC_Global.BackCheckPos = true;
        }

        private DelegateCommand _BackCheckPosUpCmd;
        public DelegateCommand BackCheckPosUpCmd =>
            _BackCheckPosUpCmd ?? (_BackCheckPosUpCmd = new DelegateCommand(ExecuteBackCheckPosUpCmd));

        void ExecuteBackCheckPosUpCmd()
        {
            WirePCToPLC_Global.BackCheckPos = false;

        }

        private DelegateCommand _LeftGoHomeDownCmd;
        public DelegateCommand LeftGoHomeDownCmd =>
            _LeftGoHomeDownCmd ?? (_LeftGoHomeDownCmd = new DelegateCommand(ExecuteLeftGoHomeDownCmd));

        void ExecuteLeftGoHomeDownCmd()
        {
            LeftMotorPCToPLC_Global.GoHome = true;
        }

        private DelegateCommand _LeftGoHomeUpCmd;
        public DelegateCommand LeftGoHomeUpCmd =>
            _LeftGoHomeUpCmd ?? (_LeftGoHomeUpCmd = new DelegateCommand(ExecuteLeftGoHomeUpCmd));

        void ExecuteLeftGoHomeUpCmd()
        {
            LeftMotorPCToPLC_Global.GoHome = false;

        }
        private DelegateCommand _LeftDirectGoDownCmd;
        public DelegateCommand LeftDirectGoDownCmd =>
            _LeftDirectGoDownCmd ?? (_LeftDirectGoDownCmd = new DelegateCommand(ExecuteLeftDirectGoDownCmd));

        void ExecuteLeftDirectGoDownCmd()
        {
            LeftMotorPCToPLC_Global.DirectGo = true;
        }
        private DelegateCommand _LeftDirectGoUpCmd;
        public DelegateCommand LeftDirectGoUpCmd =>
            _LeftDirectGoUpCmd ?? (_LeftDirectGoUpCmd = new DelegateCommand(ExecuteLeftDirectGoUpCmd));

        void ExecuteLeftDirectGoUpCmd()
        {
            LeftMotorPCToPLC_Global.DirectGo = false;

        }

        private DelegateCommand _RightGoHomeDownCmd;
        public DelegateCommand RightGoHomeDownCmd =>
            _RightGoHomeDownCmd ?? (_RightGoHomeDownCmd = new DelegateCommand(ExecuteRightGoHomeDownCmd));

        void ExecuteRightGoHomeDownCmd()
        {
            RightMotorPCToPLC_Global.GoHome = true;
        }

        private DelegateCommand _RightGoHomeUpCmd;
        public DelegateCommand RightGoHomeUpCmd =>
            _RightGoHomeUpCmd ?? (_RightGoHomeUpCmd = new DelegateCommand(ExecuteRightGoHomeUpCmd));

        void ExecuteRightGoHomeUpCmd()
        {
            RightMotorPCToPLC_Global.GoHome = false;

        }


        private DelegateCommand _RightDirextGoDownCmd;
        public DelegateCommand RightDirextGoDownCmd =>
            _RightDirextGoDownCmd ?? (_RightDirextGoDownCmd = new DelegateCommand(ExecuteRightDirextGoDownCmd));

        void ExecuteRightDirextGoDownCmd()
        {
            RightMotorPCToPLC_Global.DirectGo = true;
        }

        private DelegateCommand _RightDirextGoUpCmd;
        public DelegateCommand RightDirextGoUpCmd =>
            _RightDirextGoUpCmd ?? (_RightDirextGoUpCmd = new DelegateCommand(ExecuteRightDirextGoUpCmd));

        void ExecuteRightDirextGoUpCmd()
        {
            RightMotorPCToPLC_Global.DirectGo = false;
        }


        private DelegateCommand _ForwardDownCmd;
        public DelegateCommand ForwardDownCmd =>
            _ForwardDownCmd ?? (_ForwardDownCmd = new DelegateCommand(ExecuteForwardDownCmd));

        void ExecuteForwardDownCmd()
        {
            YAxisRightServo.servoPCToPLC_Global.bForwardBT = true;
        }


        private DelegateCommand _ForwardUpCmd;
        public DelegateCommand ForwardUpCmd =>
            _ForwardUpCmd ?? (_ForwardUpCmd = new DelegateCommand(ExecuteForwardUpCmd));

        void ExecuteForwardUpCmd()
        {
            YAxisRightServo.servoPCToPLC_Global.bForwardBT = false;

        }


        private DelegateCommand _ReverseDownCmd;
        public DelegateCommand ReverseDownCmd =>
            _ReverseDownCmd ?? (_ReverseDownCmd = new DelegateCommand(ExecuteReverseDownCmd));

        void ExecuteReverseDownCmd()
        {
            YAxisRightServo.servoPCToPLC_Global.bReverseBT = true;

        }

        private DelegateCommand _ReverseUpCmd;
        public DelegateCommand ReverseUpCmd =>
            _ReverseUpCmd ?? (_ReverseUpCmd = new DelegateCommand(ExecuteReverseUpCmd));

        void ExecuteReverseUpCmd()
        {
            YAxisRightServo.servoPCToPLC_Global.bReverseBT = false;

        }
        private DelegateCommand<string> _TextChangeCommand;
        public DelegateCommand<string> TextChangeCommand =>
            _TextChangeCommand ?? (_TextChangeCommand = new DelegateCommand<string>(ExecuteTextChangeCommand));

        void ExecuteTextChangeCommand(string p)
        {
            switch (p)
            {

                case "WireLenChangSpeed":
                    WireParam_Global.WireLenChangSpeed = WireLenChangSpeed;
                    break;
                case "LeftCheckPos":
                    LeftMotorParam_Global.CheckPosition = LeftCheckPos;
                    break;
                case "RightCheckPos":
                    RightMotorParam_Global.CheckPosition = RightCheckPos;
                    break;
                case "LeftWaitPos":
                    LeftMotorParam_Global.WaitPostion = LeftWaitPos;
                    break;
                case "RightWaitPos":
                    RightMotorParam_Global.WaitPostion = RightWaitPos;
                    break;
                case "LeftWorkPos":
                    LeftMotorParam_Global.WorkPosition = Convert.ToInt32(LeftWorkPos * 10000);
                    break;
                case "RightWorkPos":
                    RightMotorParam_Global.WorkPosition = Convert.ToInt32(RightWorkPos * 10000);
                    break;
                case "DefineSpacing":
                    WireParam_Global.WireDerictLen = DefineSpacing;
                    break;
                case "CheckPinGap":
                    WireParam_Global.CheckPinGap = CheckPinGap;
                    break;
                default:
                    break;
            }
        }
        #endregion



        #region 属性
        private float _WireCurrentLen;
        public float WireCurrentLen
        {
            get { return _WireCurrentLen; }
            set { SetProperty(ref _WireCurrentLen, value); }
        }




        private int _RightCheckPos;
        public int RightCheckPos
        {
            get { return _RightCheckPos; }
            set { SetProperty(ref _RightCheckPos, value); }
        }

        private int _LeftCheckPos;
        public int LeftCheckPos
        {
            get { return _LeftCheckPos; }
            set { SetProperty(ref _LeftCheckPos, value); }
        }


        private int _LeftPos;
        public int LeftPos
        {
            get { return _LeftPos; }
            set { SetProperty(ref _LeftPos, value); }
        }

        private int _RightPos;
        public int RightPos
        {
            get { return _RightPos; }
            set { SetProperty(ref _RightPos, value); }
        }


        private float _RightWorkPos;
        public float RightWorkPos
        {
            get { return _RightWorkPos; }
            set { SetProperty(ref _RightWorkPos, value); }
        }

        private float _LeftWorkPos;
        public float LeftWorkPos
        {
            get { return _LeftWorkPos; }
            set { SetProperty(ref _LeftWorkPos, value); }
        }

        private int _RightWaitPos;
        public int RightWaitPos
        {
            get { return _RightWaitPos; }
            set { SetProperty(ref _RightWaitPos, value); }
        }

        private int _LeftWaitPos;
        public int LeftWaitPos
        {
            get { return _LeftWaitPos; }
            set { SetProperty(ref _LeftWaitPos, value); }
        }


        private float _XCurrentPos;
        public float XCurrentPos
        {
            get { return _XCurrentPos; }
            set { SetProperty(ref _XCurrentPos, value); }
        }


        private float _YLCurrentPos;
        public float YLCurrentPos
        {
            get { return _YLCurrentPos; }
            set { SetProperty(ref _YLCurrentPos, value); }
        }

        private float _YRCurrentPos;
        public float YRCurrentPos
        {
            get { return _YRCurrentPos; }
            set { SetProperty(ref _YRCurrentPos, value); }
        }

        private float _DefineSpacing;
        public float DefineSpacing
        {
            get { return _DefineSpacing; }
            set { SetProperty(ref _DefineSpacing, value); }
        }

        private float _WireLenChangSpeed;
        public float WireLenChangSpeed
        {
            get { return _WireLenChangSpeed; }
            set { SetProperty(ref _WireLenChangSpeed, value); }
        }
        /// <summary>
        /// 校准针间距
        /// </summary>
        private float _CheckPinGap;
        public float CheckPinGap
        {
            get { return _CheckPinGap; }
            set { SetProperty(ref _CheckPinGap, value); }
        }
        #endregion
    }
}
